import React, { useState, useRef } from 'react';

const PromotionLink = ({ link, onCopySuccess }) => {
  const [copySuccess, setCopySuccess] = useState(false);
  const linkInputRef = useRef(null);

  const copyLink = () => {
    linkInputRef.current.select();
    document.execCommand('copy');
    setCopySuccess(true);
    onCopySuccess && onCopySuccess();
    setTimeout(() => setCopySuccess(false), 2000);
  };

  return (
    <div className="promotion-link-section">
      <h3 className="section-title">我的专属推广链接</h3>
      <div className="link-container">
        <input
          type="text"
          value={link}
          readOnly
          ref={linkInputRef}
          className="promotion-link"
        />
        <button onClick={copyLink} className="copy-btn">复制链接</button>
      </div>
      {copySuccess && <p className="copy-success">复制成功！</p>}
    </div>
  );
};

export default PromotionLink;
